package com.xingkeduo.business.user.domain;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.user.service.*;
import com.xingkeduo.pojo.dto.BaseDTO;
import com.xingkeduo.pojo.dto.CraftsmanDTO;
import com.xingkeduo.pojo.dto.SubscribeDTO;
import com.xingkeduo.pojo.enums.LoginEnums;
import com.xingkeduo.pojo.enums.SendSmsEnums;
import com.xingkeduo.pojo.query.*;
import com.xingkeduo.pojo.table.*;
import com.xingkeduo.pojo.utils.Base64Utils;
import com.xingkeduo.pojo.utils.MD5Utils;
import com.xingkeduo.pojo.utils.RandomNumberUtil;
import com.xingkeduo.util.pojo.domain.Credential;
import com.xingkeduo.util.pojo.enums.SexEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.BusinessException;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.IdentityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/6/3 16:51
 **/
@Slf4j
@Component
public class CraftsmanDomain {

    @Resource
    private CraftsmanService craftsmanService;
    @Resource
    private CraftsmanJobService craftsmanJobService;
    @Resource
    private CraftsmanSocialService craftsmanSocialService;
    @Resource
    private CraftsmanShopService craftsmanShopService;
    @Resource
    private CraftsmanLevelService levelService;
    @Resource
    private CraftsmanDutyService dutyService;
    @Resource
    private CraftsmanHealthService craftsmanHealthService;
    @Resource
    private CraftsmanFileService fileService;
    @Resource
    private FeignDomain feignDomain;
    @Resource
    private JobseekerSocialService jobseekerSocialService;
    @Resource
    private CraftsmanLoginHistoryService craftsmanLoginHistoryService;
    @Resource
    private CraftsmanSmsService craftsmanSmsService;


    /**
     * 登录
     *
     * @param craftsmanLoginHistoryQO:
     * @return : com.xingkeduo.pojo.table.CraftsmanSocialTO
     * @author : gaozifeng
     * @date : 2019/3/18 15:55
     */
    public Credential login(CraftsmanLoginHistoryQO craftsmanLoginHistoryQO) throws Throwable {
        log.info("登录请求参数" + JSON.toJSONString(craftsmanLoginHistoryQO));
        // 1微信授权 2手机号+验证码 3手机号+密码
        List<Long> longs = new ArrayList<>(5);
        CraftsmanTO craftsmanTO = null;
        CraftsmanSocialQO craftsmanSocialQO;

        if (LoginEnums.WECHAT.equals(craftsmanLoginHistoryQO.getType())) {
            craftsmanTO = craftsmanService.selectByLogin(craftsmanLoginHistoryQO.getLogin());
        } else if (LoginEnums.PHONE_NUM.equals(craftsmanLoginHistoryQO.getType()) || LoginEnums.PHONE_PASSWORD.equals(craftsmanLoginHistoryQO.getType())) {
            if (!StringUtils.isEmpty(craftsmanLoginHistoryQO.getMsgCode())) {
                feignDomain.checkMsg(SendSmsEnums.CRAFTSMAN_LOGIN.getType(), craftsmanLoginHistoryQO.getLogin(), craftsmanLoginHistoryQO.getMsgCode());
            }
            craftsmanSocialQO = new CraftsmanSocialQO();
            craftsmanSocialQO.setPhone(craftsmanLoginHistoryQO.getLogin()).setIsDelete(YesNoEnum.NO);
            longs = craftsmanSocialService.selectPrimaryByQuery(craftsmanSocialQO);
            if (longs.size() > 1) {
                log.info("用户[{}]登录,账号不唯一" + JSON.toJSONString(longs));
                throw new DataException(DataException.Type.NOTONLY, "");
            }
            if (longs.size() <= 0 || null == longs) {
                //查询是不是面试账号
                JobseekerSocialQO jobseekerSocialQO = new JobseekerSocialQO();
                jobseekerSocialQO.setPhone(craftsmanLoginHistoryQO.getLogin()).setIsDelete(YesNoEnum.NO);
                List<Long> jobids = jobseekerSocialService.selectPrimaryByQuery(jobseekerSocialQO);

                if (jobids != null && jobids.size() > 0) {
                    throw new DataException(DataException.Type.DEFAULT, "您尚未通过面试，无法使用星客多家园，请联系教育部老师");
                }
                throw new DataException(DataException.Type.DEFAULT, "账号不存在，请确认后输入。");
            }
            craftsmanTO = craftsmanService.selectByPrimary(longs.get(0));
        } else {
            throw new DataException(DataException.Type.DEFAULT, "登录方式错误");
        }
        if (craftsmanTO == null) {
            throw new DataException(DataException.Type.DEFAULT, "账号不存在，请确认后输入。");
        }
        if (null != craftsmanLoginHistoryQO.getPassword()) {
            boolean verify = MD5Utils.verify(craftsmanLoginHistoryQO.getPassword(), craftsmanTO.getPassword());
            if (!verify)
                throw new DataException(DataException.Type.DEFAULT, "密码错误");
        }
        if (craftsmanTO.getStatus().getValue() == CraftsmanTO.Status.LEAVE.getValue()) {
            throw new DataException(DataException.Type.DEFAULT, "您的账号已离职，无法使用星客多家园，请联系总经理");
        }
        if (longs.size() > 0)
            if (longs.get(0) != craftsmanTO.getId()) {
                throw new DataException(DataException.Type.DEFAULT, "账号密码不匹配");
            }
        Credential credential = new Credential();
        credential.setId(craftsmanTO.getId());
        credential.setStatus(craftsmanTO.getRegisterStep());
        CraftsmanLoginHistoryTO craftsmanLoginHistoryTO = new CraftsmanLoginHistoryTO(craftsmanLoginHistoryQO.getLogin(),
                craftsmanLoginHistoryQO.getPassword(), String.valueOf(craftsmanLoginHistoryQO.getType().getType()));
        craftsmanLoginHistoryService.insert(craftsmanLoginHistoryTO);
        return credential;
    }


    /**
     * 发送短信的
     *
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/3 21:19
     */
    @Transactional
    public void sendSms(CraftsmanSocialQO socialQO) throws Throwable {

        log.info("发短信type:" + JSON.toJSONString(socialQO));
        if (StringUtils.isEmpty(socialQO.getPhone())) {
            throw new DataException(DataException.Type.DEFAULT, "手机号不能为空");
        }
        if (2 == socialQO.getType()) {
            if (socialQO.getId() <= 0) {
                throw new DataException(DataException.Type.DEFAULT, "账号信息不能为空");
            }
        }
        CraftsmanSocialQO phone = new CraftsmanSocialQO();
        phone.setPhone(socialQO.getPhone());
        List<CraftsmanSocialTO> longs = craftsmanSocialService.selectByQuery(phone);
        long id = 0;
        if (SendSmsEnums.CRAFTSMAN_UPDATE_PASSWORD.getType() == socialQO.getType() || SendSmsEnums.CRAFTSMAN_LOGIN.getType() == socialQO.getType()) {
            if (null == longs || longs.size() <= 0) {
                List<Long> list = jobseekerSocialService.selectPrimaryByQuery(new JobseekerSocialQO().setPhone(socialQO.getPhone()));
                if (null == list || list.size() <= 0) {
                    throw new DataException(DataException.Type.DEFAULT, "手机号不存在");
                } else {
                    throw new DataException(DataException.Type.DEFAULT, "您尚未通过面试，无法使用星客多家园，请联系教育部老师.");
                }
            }
            id = longs.get(0).getId();
        } else {
            //更换手机号
            if (null != longs && longs.size() > 0) {
                throw new DataException(DataException.Type.DEFAULT, "手机号已经被绑定");
            }
            id = socialQO.getId();
        }

        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(id);
        if (craftsmanTO.getStatus().getValue() != CraftsmanTO.Status.BIND.getValue() && craftsmanTO.getStatus().getValue() != CraftsmanTO.Status.FLOW.getValue() && craftsmanTO.getStatus().getValue() != CraftsmanTO.Status.STUDENT.getValue()) {
            throw new DataException(DataException.Type.DEFAULT, "此账号不是发型师");
        }
        //发短信
        String randomNum = RandomNumberUtil.randomNum();
        feignDomain.sendMsg(socialQO.getType(), socialQO.getPhone(), randomNum);
        //新增验证码到发型师库里面
        CraftsmanSmsTO sms = new CraftsmanSmsTO();
        sms.setCraftsmanId(craftsmanTO.getId()).setPhone(socialQO.getPhone()).setIsDelete(YesNoEnum.NO).setSmsCode(randomNum).setSmsType("1");
        craftsmanSmsService.insert(sms);
        log.info("user ---------------send/sms --------------- 发送成功");

    }

    /**
     * 修改密码
     *
     * @param craftsmanQO: 此处login 为新手机号.
     * @return : void
     * @author : gaozifeng
     * @date : 2019/3/18 21:33
     */
    @Transactional
    public void updatePassWord(CraftsmanQO craftsmanQO) throws Throwable {
        if (StringUtils.isEmpty(craftsmanQO.getMsgCode())) {
            throw new DataException(DataException.Type.DEFAULT, "验证码不能为空");
        }
        List<Long> longs = craftsmanSocialService.selectPrimaryByPhone(craftsmanQO.getLogin());
        if (null == longs || longs.size() <= 0) {
            throw new DataException(DataException.Type.DEFAULT, "此手机号不存在");
        }
        List<CraftsmanSmsTO> craftsmanSmsTOS = craftsmanSmsService.selectList(new CraftsmanSmsQO().setPhone(craftsmanQO.getLogin()).setSmsCode(craftsmanQO.getMsgCode()).setSmsType("1"));
        if (null == craftsmanSmsTOS || craftsmanSmsTOS.size() <= 0) {
            throw new DataException(DataException.Type.DEFAULT, "短信和手机号没有记录");
        }
        feignDomain.checkMsg(SendSmsEnums.CRAFTSMAN_UPDATE_PASSWORD.getType(), craftsmanQO.getLogin(), craftsmanQO.getMsgCode());

        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(longs.get(0));
        if (craftsmanTO.getStatus().getValue() != CraftsmanTO.Status.BIND.getValue() && craftsmanTO.getStatus().getValue() != CraftsmanTO.Status.FLOW.getValue() && craftsmanTO.getStatus().getValue() != CraftsmanTO.Status.STUDENT.getValue()) {
            throw new DataException(DataException.Type.DEFAULT, "此账号不是发型师");
        }
        String md5 = MD5Utils.md5(craftsmanQO.getNewPassWord());
        log.info("更改的密码,md5[{}],", md5);
        craftsmanTO.setPassword(md5);
        craftsmanService.update(craftsmanTO);
    }


    /**
     * 修改发型师信息
     *
     * @return : void
     * @author : gaozifeng
     * @date : 2019/3/19 14:30
     */
    @Transactional
    public void modifyCraftsmanSocial(CraftsmanTO craftsmanTO) throws Throwable {
        CraftsmanSocialTO cst = craftsmanSocialService.selectByPrimary(craftsmanTO.getId());
        if (null == cst) {
            throw new DataException(DataException.Type.DEFAULT, "账号不存在");
        }
        if (craftsmanTO.getRegisterStep() == 1) {
            //完善艺名
            if (null == craftsmanTO.getJob().getNickName() || "".equals(craftsmanTO.getJob().getNickName())) {
                craftsmanTO.getJob().setNickName(cst.getName());
            } else {
                craftsmanTO.getJob().setNickName(craftsmanTO.getJob().getNickName());
            }
            CraftsmanJobTO jobTO = craftsmanJobService.selectByPrimary(craftsmanTO.getId());
            jobTO.setNickName(craftsmanTO.getJob().getNickName());
            craftsmanJobService.update(jobTO);
        } else if (craftsmanTO.getRegisterStep() == 2) {
            cst.setBank(craftsmanTO.getSocial().getBack());
            cst.setBankCard(craftsmanTO.getSocial().getBankCard());
            cst.setSubBranch(craftsmanTO.getSocial().getSubBranch());
            craftsmanSocialService.update(cst);
        } else {
            throw new DataException(DataException.Type.DEFAULT, "审核中");
        }
        craftsmanTO.setRegisterStep(craftsmanTO.getRegisterStep() + 1);
        CraftsmanFileQO craftsmanFileQO = new CraftsmanFileQO();
        craftsmanFileQO.setJobseeker_id(craftsmanTO.getId()).setIsDelete(YesNoEnum.NO).setTypes(craftsmanTO.getFiles().stream().map(a -> a.getType()).collect(Collectors.toList()));
        List<CraftsmanFileTO> fileList = fileService.selectForList(craftsmanFileQO);
        if (null != fileList && fileList.size() > 0) {
            List<CraftsmanFileTO> collect = fileList.stream().map(a -> a.setIsDelete(YesNoEnum.YES)).collect(Collectors.toList());
            int retVal = fileService.updateList(collect);
            if (retVal != fileList.size()) {
                throw new DataException(DataException.Type.FAIL, "文件修改失败");
            }
        }
        if (null != craftsmanTO.getPassword()) {
            String md5 = MD5Utils.md5(craftsmanTO.getPassword());
            log.info("密码加密,md5[{}],", md5);
            craftsmanTO.setPassword(md5);
        }
        craftsmanService.update(craftsmanTO);
    }

    /**
     * 检查发型师的健康证
     *
     * @param query:
     * @return : com.xingkeduo.pojo.table.CraftsmanHealthTO
     * @author : gaozifeng
     * @date : 2019/3/20 15:53
     */
    public CraftsmanHealthTO ckeckHealth(CraftsmanHealthQO query) throws Throwable {
        List<CraftsmanHealthTO> healthList = craftsmanHealthService.selectByQuery(query);
        if (null == healthList || healthList.size() <= 0) {
            throw new DataException(DataException.Type.DEFAULT, "尚未上传健康证");
        }
        List<CraftsmanHealthTO> collect = healthList.stream().filter(a -> a.getIsDelete() != YesNoEnum.YES).collect(Collectors.toList());
        if (null == collect || collect.size() <= 0) {
            return healthList.get(0);
        }
        return collect.get(0);
    }

    /**
     * 新增健康证和健康证信息
     *
     * @param healthTO:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/3/20 19:27
     */
    public void insertHealth(CraftsmanHealthTO healthTO) throws Throwable {
        List<CraftsmanHealthTO> health = craftsmanHealthService.selectByQuery(new CraftsmanHealthQO().setCraftsman_id(healthTO.getCraftsman_id()).setIsDelete(YesNoEnum.NO));
        if (null != health && health.size() > 0) {
            throw new DataException(DataException.Type.DEFAULT, "您已经上传过健康证");
        }
        craftsmanHealthService.insert(healthTO);
    }


    /**
     * 发型师端更改手机号
     *
     * @param craftsmanQO:
     * @param craftsmanSocialQO:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/3/29 16:30
     */
    public void changeCraftsmanPhone(CraftsmanQO craftsmanQO, CraftsmanSocialQO craftsmanSocialQO) throws Throwable {
        List<Long> longs = craftsmanSocialService.selectPrimaryByPhone(craftsmanSocialQO.getPhone());
        if (null != longs && longs.size() > 0) {
            throw new DataException(DataException.Type.DEFAULT, "此手机号已绑定其他账号");
        }
        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(craftsmanQO.getId());
        if (null == craftsmanTO) {
            throw new DataException(DataException.Type.DEFAULT, "账号不存在");
        }
        CraftsmanSocialTO socialTO = craftsmanSocialService.selectByPrimary(craftsmanTO.getId());
        if (null == socialTO || socialTO.getId() <= 0) {
            throw new DataException(DataException.Type.DEFAULT, "发型师信息记录不存在");
        }
        feignDomain.checkMsg(SendSmsEnums.CRAFTSMAN_UPDATE_PHONE.getType(), craftsmanSocialQO.getPhone(), String.valueOf(craftsmanSocialQO.getCode()));
        craftsmanSocialService.update(socialTO.setPhone(craftsmanSocialQO.getPhone()));
    }

    /**
     * 发型师端更改微信账号
     *
     * @param craftsmanQO:
     * @param dto:
     * @return : void
     * @author : gaozifeng
     * @date : 2019年3月29日17:09:42
     */
    @Transactional
    public void changeCraftsmanOpenid(CraftsmanQO craftsmanQO, SubscribeDTO dto) throws Throwable {

        log.info("登录id" + craftsmanQO.getId());
        CraftsmanTO manTO = craftsmanService.selectByPrimary(craftsmanQO.getId());
        if (null == manTO) {
            throw new DataException(DataException.Type.DEFAULT, "账号不存在");
        }
        log.info("dto:------------" + dto.toString());
        if (null == dto.getUnionId() || null == dto.getOpenId() || null == dto.getNickname()) {
            throw new DataException(DataException.Type.DEFAULT, "更改的用户信息为空");
        }
        CraftsmanTO craftsmanTO = craftsmanService.selectByLogin(dto.getUnionId() != null ? dto.getUnionId() : dto.getOpenId());
        if (null != craftsmanTO && craftsmanTO.getId() > 0) {
            throw new DataException(DataException.Type.DEFAULT, "此微信已绑定其他账号");
        }
        CraftsmanJobTO jobTO = craftsmanJobService.selectByPrimary(manTO.getId());
        if (null == jobTO) {
            throw new DataException(DataException.Type.DEFAULT, "账号不存在");
        }
        CraftsmanSocialTO socialTO = craftsmanSocialService.selectByPrimary(manTO.getId());
        if (null == socialTO) {
            throw new DataException(DataException.Type.DEFAULT, "账号信息不存在");
        }
        //请求城市
        long country = feignDomain.selectCity(dto.getCity());
        jobTO.setCityId(country);

        socialTO.setWechatName(Base64Utils.encodeParam(dto.getNickname())).setSex(SexEnum.MALE.getValue() == dto.getSexId() ? SexEnum.MALE : SexEnum.FEMALE);

        manTO.setLogin(dto.getUnionId() == null ? dto.getOpenId() : dto.getUnionId());
        craftsmanService.update(manTO);

        craftsmanSocialService.update(socialTO);
        if (country > 0) {
            craftsmanJobService.update(jobTO);
        }
    }

    /**
     * 获取发型师详细信息
     *
     * @param craftsmanQO:
     * @return : com.xingkeduo.pojo.table.CraftsmanSocialTO
     * @author : gaozifeng
     * @date : 2019/3/19 13:57
     */
    public CraftsmanTO getCraftsmanSocial(CraftsmanQO craftsmanQO) throws Throwable {
        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(craftsmanQO.getId());
        if (null == craftsmanTO || craftsmanTO.getId() <= 0)
            throw new DataException(DataException.Type.DEFAULT, "发型师不存在");
        CraftsmanSocialTO craftsmanSocialTO = craftsmanSocialService.selectByPrimary(craftsmanQO.getId());
        CraftsmanJobTO jobTO = craftsmanJobService.selectByPrimary(craftsmanQO.getId());
        if (jobTO != null) {
            CraftsmanLevelTO craftsmanLevelTO = levelService.selectByPrimary(jobTO.getLevelId());
            craftsmanTO.setCraftsmanLevelTO(craftsmanLevelTO);
            CraftsmanDutyTO craftsmanDutyTO = dutyService.selectByPrimary(jobTO.getDutyId());
            if (null == craftsmanDutyTO || craftsmanDutyTO.getSequence() <= 0) {
                throw new DataException(DataException.Type.DEFAULT, "岗位不存在");
            }
            craftsmanTO.setDuty(craftsmanDutyTO);

            //判断门店
            if (craftsmanDutyTO.getSequence() == 2) {
                List<CraftsmanShopTO> shopTOList = getCraftsmanShopForList(new CraftsmanShopQO().setCraftsmanId(craftsmanTO.getId()).setIsDelete(YesNoEnum.NO));
                List<Long> shopIds = shopTOList.stream().filter(a -> a.getCraftsmanId() > 0).map(a -> a.getShopId()).collect(Collectors.toList());
                craftsmanTO.setShopIds(shopIds);
            }
        }

        List<CraftsmanHealthTO> health = craftsmanHealthService.selectByQuery(new CraftsmanHealthQO().setCraftsman_id(craftsmanQO.getId()));
        List<CraftsmanFileTO> fileList = fileService.selectForList(new CraftsmanFileQO().setJobseeker_id(craftsmanQO.getId()));
        List<CraftsmanFileTO> orderByList = fileList.stream().sorted(Comparator.comparing(CraftsmanFileTO::getType)).collect(Collectors.toList());
        //TODO 健康证待更换成对象
        craftsmanTO.setFiles(orderByList).setHealthList(health).setJob(jobTO).setSocial(craftsmanSocialTO);


        return craftsmanTO;
    }

    /**
     * 获取发型师详细信息
     *
     * @param craftsmanQO:
     * @return : com.xingkeduo.pojo.table.CraftsmanSocialTO
     * @author : gaozifeng
     * @date : 2019/3/19 13:57
     */
    public CraftsmanDTO getCraftsmanSocialInfo(CraftsmanQO craftsmanQO) throws Throwable {
        craftsmanQO.setIsDelete(YesNoEnum.NO);
        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(craftsmanQO.getId());
        if (null == craftsmanTO || craftsmanTO.getId() <= 0)
            throw new DataException(DataException.Type.DEFAULT, "发型师不存在");
        if (craftsmanTO.getStatus() == CraftsmanTO.Status.LEAVE) {
            throw new DataException(DataException.Type.DEFAULT, "发型师已离职");
        }
        CraftsmanSocialTO craftsmanSocialTO = craftsmanSocialService.selectByPrimary(craftsmanQO.getId());
        if (null == craftsmanSocialTO) {
            throw new DataException(DataException.Type.NULL, "发型师个人信息");
        }
        CraftsmanJobTO jobTO = craftsmanJobService.selectByPrimary(craftsmanQO.getId());
        if (jobTO == null) {
            throw new DataException(DataException.Type.NULL, "发型师工作信息");
        }
        CraftsmanDTO craftsmanDTO = new CraftsmanDTO();
        if (StringUtils.isNotBlank(craftsmanSocialTO.getName())) {
            craftsmanDTO.setName(craftsmanSocialTO.getName());
        } else {
            craftsmanDTO.setName("小星星");
        }
        if (StringUtils.isNotBlank(jobTO.getNickName())) {
            craftsmanDTO.setNickName(jobTO.getNickName());
        } else {
            craftsmanDTO.setNickName(craftsmanDTO.getName());
        }
        List<CraftsmanFileTO> fileList = fileService.selectForList(new CraftsmanFileQO().setJobseeker_id(craftsmanTO.getId()).setType("1").setIsDelete(YesNoEnum.NO));
        if (null != fileList && fileList.size() > 0) {
            craftsmanDTO.setPath(fileList.get(0).getPath());
        }
        if (StringUtils.isNotEmpty(craftsmanSocialTO.getPhone())) {
            craftsmanDTO.setPhone(craftsmanSocialTO.getPhone());
        }
        if (0L != jobTO.getShopId()) {
            craftsmanDTO.setShopId(jobTO.getShopId());
        }
        if (null != craftsmanSocialTO.getSex()) {
            craftsmanDTO.setSex_value(craftsmanSocialTO.getSex().getValue());
            craftsmanDTO.setSex_desc(craftsmanSocialTO.getSex().getDesc());
        }
        craftsmanDTO.setId(craftsmanTO.getId());
        craftsmanDTO.setCode(craftsmanTO.getCode());
        return craftsmanDTO;
    }

    /**
     * 编辑发型师信息 经理可以修改发型师名称,员工自己不可以修改
     *
     * @param cto:
     * @param healthTO:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/4/12 15:19
     */
    @Transactional
    public void editCraftsmanInfo(CraftsmanTO cto, CraftsmanHealthTO healthTO) throws Throwable {
        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(cto.getId());
        if (null == craftsmanTO || craftsmanTO.getId() <= 0) {
            throw new DataException(DataException.Type.DEFAULT, "未找到在职发型师");
        }
        CraftsmanJobTO jobTO = craftsmanJobService.selectByPrimary(craftsmanTO.getId());
        CraftsmanDutyTO dutyTO = dutyService.selectByPrimary(jobTO.getDutyId());
        if (null == dutyTO || dutyTO.getId() <= 0) {
            throw new DataException(DataException.Type.DEFAULT, "未查询到当前岗位信息");
        }
        //TODO 需要新增判断是否修改过，不然每次改库，
        CraftsmanSocialTO socialTO = craftsmanSocialService.selectByPrimary(craftsmanTO.getId());
        socialTO.setBankCard(null != cto.getSocial().getBankCard() ? cto.getSocial().getBankCard() : "");
        socialTO.setBank(null != cto.getSocial().getBack() ? cto.getSocial().getBack() : null);
        socialTO.setSubBranch(null != cto.getSocial().getSubBranch() ? cto.getSocial().getSubBranch() : "");
        socialTO.setName(null != cto.getSocial().getName() ? cto.getSocial().getName() : "");
        //新增修改身份证号码
        socialTO.setIdCard(null != cto.getSocial().getIdCard() ? cto.getSocial().getIdCard() : "");
        //大于发型师 可以修改名字，艺名
        if (!cto.getJob().getNickName().equals(jobTO.getNickName())) {
            jobTO.setNickName(cto.getJob().getNickName());
            craftsmanJobService.update(jobTO);
        }
        craftsmanSocialService.update(socialTO);

        List<CraftsmanFileTO> fileList = fileService.selectForList(new CraftsmanFileQO().setJobseeker_id(craftsmanTO.getId()).setIsDelete(YesNoEnum.NO));
        //修改健康证的
        List<CraftsmanHealthTO> healthList = craftsmanHealthService.selectByQuery(new CraftsmanHealthQO().setCraftsman_id(craftsmanTO.getId()).setIsDelete(YesNoEnum.NO));
        if (healthList.size() > 0) {
            CraftsmanHealthTO newHealth = healthList.get(0);
            newHealth.setHealthNum(healthTO.getHealthNum()).setEffectiveTime(healthTO.getEffectiveTime()).setFinishTime(healthTO.getFinishTime());
            craftsmanHealthService.update(newHealth);
        }
        //修改
        if (fileList.size() > 0 && null != fileList) {
            List<CraftsmanFileTO> updateList = new ArrayList<>(10);
            for (int i = 0; i < cto.getFiles().size(); i++) {
                for (int j = 0; j < fileList.size(); j++) {
                    if (cto.getFiles().get(i).getType().equals(fileList.get(j).getType())) {
                        log.info("要修改的值" + fileList.get(j).toString());
                        fileList.get(j).setIsDelete(YesNoEnum.YES);
                        updateList.add(fileList.get(j));
                    }
                }
            }
            int retVal = fileService.updateList(updateList);
            if (retVal != updateList.size()) {
                throw new DataException(DataException.Type.DEFAULT, "修改附件失败");
            }
        }

        for (CraftsmanFileTO fileTO : cto.getFiles()) {
            fileTO.setId(IdentityUtil.nextId(IdentityUtil.Worker.USER));
        }
        fileService.insertList(cto.getFiles());
    }

    /**
     * 获取经理门店列表
     *
     * @param craftsmanQO:
     * @return : java.util.List<java.lang.Long>
     * @author : gaozifeng
     * @date : 2019/6/3 17:00
     */
    public BaseDTO getShopIdForList(CraftsmanQO craftsmanQO) throws Throwable {
        CraftsmanTO craftsmanTO = getCraftsmanById(craftsmanQO);

        CraftsmanShopQO craftsmanShopQO = new CraftsmanShopQO();
        craftsmanShopQO.setCraftsmanId(craftsmanTO.getId()).setIsDelete(YesNoEnum.NO);
        List<CraftsmanShopTO> shopTOList = getCraftsmanShopForList(craftsmanShopQO);

        List<Long> shopIds = shopTOList.stream().filter(a -> a.getCraftsmanId() > 0).map(a -> a.getShopId()).collect(Collectors.toList());
        BaseDTO dto = new BaseDTO();
        dto.setIds(shopIds);
        return dto;
    }


    /**
     * 注册步骤，用于初始查询，再登录
     *
     * @param craftsmanQO:
     * @return : com.xingkeduo.pojo.table.CraftsmanTO
     * @author : gaozifeng
     * @date : 2019/4/8 14:24
     */
    public CraftsmanTO checkRegister(CraftsmanQO craftsmanQO) throws Throwable {
        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(craftsmanQO.getId());
        if (null == craftsmanTO) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "数据不存在");
        }
        return craftsmanTO;
    }


    /**
     * 查询发型师手机号是否存在
     *
     * @param phone:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/4/9 11:09
     */
    public void checkUserPhone(String phone) throws Throwable {
        List<Long> longs = craftsmanSocialService.selectPrimaryByPhone(phone);
        if (null == longs || longs.size() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "手机号不存在");
        }
    }

    private CraftsmanTO getCraftsmanById(CraftsmanQO craftsmanQO) throws Throwable {
        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(craftsmanQO.getId());
        if (null == craftsmanTO) {
            throw new DataException(DataException.Type.DEFAULT, "发型师不存在");
        }
        if (CraftsmanTO.Status.LEAVE == craftsmanTO.getStatus()) {
            throw new DataException(DataException.Type.DEFAULT, "发型师已离职");
        }
        return craftsmanTO;
    }

    private List<CraftsmanShopTO> getCraftsmanShopForList(CraftsmanShopQO craftsmanShopQO) throws Throwable {
        List<CraftsmanShopTO> shopTOList = craftsmanShopService.selectByQuery(craftsmanShopQO);
        //可能存在没有管理店铺的店经理
//        if (null == shopTOList || shopTOList.size() <= 0) {
//            log.info("查询发型师公用------------");
//            throw new DataException(DataException.Type.DEFAULT, "没有查询到发型师门店列表");
//        }
        return shopTOList;
    }


    /**
     * 手动添加 发型师
     *
     * @param craftsman:
     * @param craftsmanJob:
     * @param craftsmanSocial:
     * @param craftsmanDutyQO:
     * @param craftsmanLevelQO:
     * @param operator:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/8/16 17:49
     */
    public void cmsInsertCraftsmanInfo(CraftsmanTO craftsman, CraftsmanJobTO craftsmanJob, CraftsmanSocialTO craftsmanSocial, CraftsmanDutyQO craftsmanDutyQO, CraftsmanLevelQO craftsmanLevelQO, long operator) throws Throwable {

        CraftsmanDutyTO craftsmanDutyTO = dutyService.selectByPrimary(craftsmanDutyQO.getId());
        if (null == craftsmanDutyTO) {
            throw new DataException(DataException.Type.DEFAULT, "发型师岗位不存在");
        }
        CraftsmanLevelTO craftsmanLevelTO = levelService.selectByPrimary(craftsmanLevelQO.getId());
        if (null == craftsmanLevelTO) {
            throw new DataException(DataException.Type.DEFAULT, "发型师职级不存在");
        }
        long id = IdentityUtil.nextId(IdentityUtil.Worker.USER);
        craftsman.setId(id).setStatus(CraftsmanTO.Status.BIND).setIsDelete(YesNoEnum.NO).setType(CraftsmanTO.Type.HAIRCUT).setCreatedTime(new Date()).setCreatedBy(operator);

        craftsmanJob.setId(id).setFormalTime(new Date()).setCreatedTime(new Date()).setIsDelete(YesNoEnum.NO).setCreatedBy(operator).setDutyId(craftsmanDutyTO.getSequence()).setLevelId(craftsmanLevelTO.getId());

        craftsmanSocial.setId(id).setCreatedTime(new Date()).setCreatedBy(operator).setIsDelete(YesNoEnum.NO).setVersion(1);

        craftsmanService.insert(craftsman);

        craftsmanJobService.insert(craftsmanJob);

        craftsmanSocialService.insert(craftsmanSocial);

    }
}
